home *** CD-ROM | disk | FTP | other *** search
- Path: dur-news.ctron.com!dur-news!grieve
- From: grieve@ATM-ws42 (David Grieve)
- Newsgroups: comp.lang.c++
- Subject: Re: Two way communication between objects
- Date: 30 Jan 1996 20:52:09 GMT
- Organization: Cabletron Systems, Inc.
- Distribution: world
- Message-ID: <GRIEVE.96Jan30155209@ATM-ws42>
- References: <310ACCE2.2600@werple.mira.net.au>
- <ENNO.96Jan28110653@kitz.inferenzsysteme.informatik.th-darmstadt.de>
- NNTP-Posting-Host: atm-ws42.ctron.com
- In-reply-to: enno@inferenzsysteme.informatik.th-darmstadt.de's message of 28 Jan 1996 10:06:52 GMT
-
- In article <310ACCE2.2600@werple.mira.net.au> Ross Forder <erosco@werple.mira.net.au> writes:
-
- I have to apologise what what is probably a stupid question but I'm only
- a beginner at some of this stuff.
-
- I am trying to write a VERY simple client and server set of objects. I
- would like to client to register with the server at ctor time and have
- the server know about the client by saving a pointer to the client
- so he can callback to a method called say 'event'.
-
- The problem is the fact that they will both need a pointer to each other
- and this seems impossible using strong typing. Is there a simple 'object
- oriented' way to do this?
-
- I have been able to make this work by having the server only know about
- a parent class of the client (say eventhandler) but this is still not a
- bulletproof solution.
-
- Can someone set me straight?
-
- Maybe I don't understand the problem. How about something like this
- (although the objects are strongly coupled). This could easily be
- extended to a n:1 client-server relationship.
-
- class Client;
-
- class Server;
- {
- private:
- Client* client;
- public:
- Server();
- void register_client(Client* clnt) { client = clnt; }
- void notify_client() { client->event(); }
- };
-
- class Client
- {
- private:
- Server* server;
- public:
- Client(Server* srvr)
- {
- server = srvr;
- server->register_client(this);
- }
-
- void event() { cout << "foobar!"; }
- };
-
- main()
- {
- Server s;
- Client c(&s);
-
- s.notify_client();
- }
-
-
- --
- David Grieve | grieve@ctron.com
- Cabletron Systems, Inc. | 603-337-7622
- Durham, NH USA 03824 | Opinions expressed are my wife's.
-